﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace PascalConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            ConvertFilenames(args[0]);
            ConvertUrls(args[0]);
        }

        private static void ConvertFilenames(string rootPath)
        {
            var manager = new FileManager() { rootPath = rootPath };

            var fileNames = manager.GetAllFileNames();
            var files = manager.GetFiles();

            bool skip = false;
            int numReplacements = 0;
            string nameToUse = String.Empty;

            foreach (string fileName in fileNames)
            {
                Console.WriteLine(fileName);
                string correctedFileName = Console.ReadLine();
                if (correctedFileName == "")
                {
                    // Already correct, do work.
                    nameToUse = fileName;
                }
                else if (correctedFileName == "s")
                {
                    skip = true;
                }
                else
                {
                    // Use correctedFileName
                    nameToUse = correctedFileName;
                }

                nameToUse += ".asp";

                if (!skip)
                {
                    bool contains = false;
                    foreach (var file in files)
                    {
                        var fileContents = manager.ReadFile(file);
                        string[] newFileContents = new string[fileContents.Length];
                        for (int i = 0; i < fileContents.Length; i++)
                        {
                            string line = fileContents[i];
                            if (line.ContainsIgnoreCase(nameToUse))
                            {
                                newFileContents[i] = line.ReplaceIgnoreCaseAndConvertToPascal(nameToUse);
                                contains = true;
                                numReplacements++;
                            }
                            else
                            {
                                newFileContents[i] = line;
                            }
                        }
                        if (contains)
                            manager.WriteFile(file, newFileContents);
                        contains = false;
                    }
                    Console.WriteLine("Replaced " + nameToUse + ": " + numReplacements + " times.\n");
                    numReplacements = 0;
                }
                skip = false;
            }
            Console.WriteLine("ConvertFilenames() has completed. Now starting ConvertUrls().\n");
        }

        private static void ConvertUrls(string rootPath)
        {
            var manager = new FileManager() { rootPath = rootPath };

            var files = manager.GetFiles();
            var directoryNames = manager.GetAllDirectoryNames();

            bool skip = false;
            int numReplacements = 0;
            string nameToUse = String.Empty;

            foreach (string directoryName in directoryNames)
            {
                Console.WriteLine(directoryName);
                string correctedDirectoryName = Console.ReadLine();
                if (correctedDirectoryName == "")
                {
                    // Already correct, do work.
                    nameToUse = directoryName;
                }
                else if (correctedDirectoryName == "s")
                {
                    skip = true;
                }
                else
                {
                    // Use correctedFileName
                    nameToUse = correctedDirectoryName;
                }

                if (!skip)
                {
                    bool contains = false;
                    foreach (var file in files)
                    {
                        var fileContents = manager.ReadFile(file);
                        string[] newFileContents = new string[fileContents.Length];
                        for (int i = 0; i < fileContents.Length; i++)
                        {
                            string line = fileContents[i];
                            if (line.ContainsIgnoreCase("/" + nameToUse + "/") && !line.Contains("<!--"))
                            {
                                newFileContents[i] = line.ReplaceIgnoreCase(nameToUse);
                                contains = true;
                                numReplacements++;
                            }
                            else
                            {
                                newFileContents[i] = line;
                            }
                        }
                        if (contains)
                            manager.WriteFile(file, newFileContents);
                        contains = false;
                    }
                    Console.WriteLine("Replaced " + nameToUse + ": " + numReplacements + " times.\n");
                    numReplacements = 0;
                }
                skip = false;
            }

            Console.WriteLine("ConvertUrls() has completed. All work is finished.");
        }
    }
}
